package com.handpay.zztong.hp.main.model.component;

import android.content.Context;
import android.content.res.AssetManager;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * Created by lyang on 2017/4/10.
 */

public class ComponentConfigUtil {

    public static  ArrayList<ComponentItemBean> getComponentListFromConfig(Context context){
        ArrayList<ComponentItemBean> list = null;
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            ComponentItemHandler handler = new ComponentItemHandler();
            reader.setContentHandler(handler);
            AssetManager assetManager = context.getResources().getAssets(); //获取管理assets目录资源的AssetManager引用
            InputStream inputStream = assetManager.open("init_component_info.xml"); //获取assets/data.txt资源
            InputSource is = new InputSource(inputStream);//取得本地xml文件
            reader.parse(is);
            list = handler.getComponentList();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    public static  ArrayList<ComponentMappingItemBean> getComponentMappingListFromConfig(Context context){
        ArrayList<ComponentMappingItemBean> list = null;
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            ComponentMappingItemHandler handler = new ComponentMappingItemHandler();
            reader.setContentHandler(handler);
            AssetManager assetManager = context.getResources().getAssets(); //获取管理assets目录资源的AssetManager引用
            InputStream inputStream = assetManager.open("init_component_mapping_info.xml"); //获取assets/data.txt资源
            InputSource is = new InputSource(inputStream);//取得本地xml文件
            reader.parse(is);
            list = handler.getComponentMappingList();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    public static  ArrayList<ComponentRelationItemBean> getComponentRelationListFromConfig(Context context){
        ArrayList<ComponentRelationItemBean> list = null;
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            ComponentRelationItemHandler handler = new ComponentRelationItemHandler();
            reader.setContentHandler(handler);
            AssetManager assetManager = context.getResources().getAssets(); //获取管理assets目录资源的AssetManager引用
            InputStream inputStream = assetManager.open("init_component_relation_info.xml"); //获取assets/data.txt资源
            InputSource is = new InputSource(inputStream);//取得本地xml文件
            reader.parse(is);
            list = handler.getComponentRelationList();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
